{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a223648a",
   "metadata": {},
   "outputs": [],
   "source": [
    "##文件加载\n",
    "##向量库处理\n",
    "##查询回答"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "139ed34c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import Docx2txtLoader\n",
    "from langchain.document_loaders import PyPDFLoader\n",
    "from langchain.document_loaders import UnstructuredExcelLoader\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.vectorstores import Chroma\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "\n",
    "class ChatDoc():\n",
    "    def __init__(self):\n",
    "        self.doc=None\n",
    "        self.split=[]\n",
    "    def getfile(self):\n",
    "        doc=self.doc\n",
    "        loaders=[\n",
    "        {\"pdf\":PyPDFLoader},\n",
    "        {\"docx\":Docx2txtLoader},\n",
    "        {\"xlsx\":UnstructuredExcelLoader}\n",
    "        ]\n",
    "        file_ex=doc.split(\".\")[-1]\n",
    "        loader=loaders.get(file_ex)\n",
    "        if loader:\n",
    "            try:\n",
    "                text=loader.load(doc)\n",
    "                return text\n",
    "            except Exception as e:\n",
    "                print(f\"loading error:{e}\")\n",
    "        else:\n",
    "            print(f\"不支持的文件格式\")\n",
    "    def split_doc(self):\n",
    "        text=self.getfile()\n",
    "        if text !=None:\n",
    "            text_spliter=CharacterTextSplitter(chunk_size=100,chunk_overlap=20)\n",
    "            texts=text_spliter.split_documents(text)\n",
    "            self.split=texts\n",
    "    def get_vector(self):\n",
    "        if not self.split:\n",
    "            self.split_doc()\n",
    "        if self.split:\n",
    "            embeddings=OpenAIEmbeddings()\n",
    "            vectorstore=Chroma.from_documents(documents=self.split,embedding=embeddings)\n",
    "            return vectorstore\n",
    "        else:\n",
    "            print(\"没有可用的文本进行向量化处理\")\n",
    "    def query(self,query):\n",
    "        vectorstore=self.get_vector()\n",
    "        retriever=vectorstore.as_retriever()\n",
    "        results=retriever.invoke(query)\n",
    "        if results:\n",
    "            return results\n",
    "        else:\n",
    "            print(\"没有找到相关内容\")   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7625dcf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
